package com.itheima.UDPdemo.UDP三种通信方式.组播;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveMessageDemo1 {
    public static void main(String[] args) throws IOException {
        //1.创建MulticastSocket对象
        MulticastSocket ms=new MulticastSocket(10086);

        //2.把本机划为224.0.0.1的一组中
        InetAddress address=InetAddress.getByName("224.0.0.1");
        ms.joinGroup(address);
        //3.正常行为
        byte[] bytes=new byte[1024];
        DatagramPacket dp=new DatagramPacket(bytes,bytes.length);//每一次都会覆盖

        ms.receive(dp);

        byte[] bytes1=dp.getData();

        System.out.println(new String(bytes1,0,dp.getLength()));
        System.out.println(dp.getLength());
        System.out.println(dp.getAddress());
        System.out.println(dp.getPort());

        ms.close();



    }
}
